var express = require('express');
var router = express.Router();
var { } = require('../module/wangdongdong')
var { goods, category } = require('../module/wangzibo')

// 获取商品
router.get('/goods', async (req, res) => {
    const { goodsId } = req.query;
    
    // 如果有 goodsId 参数，查询特定商品
    if (goodsId) {
        const data = await goods.findById(goodsId).populate('categroyId');
        if (!data) {
            return res.send({
                code: 404,
                message: '商品不存在'
            });
        }
        return res.send({
            code: 200,
            data: data
        });
    }
} )

// 获取推荐商品
router.get('/recommend', async (req, res) => {
    try {
        const { goodsId } = req.query;

        // 查询除了当前商品以外的所有商品
        const recommendGoods = await goods.find({
            _id: { $ne: goodsId } // 只排除当前商品
        });

        res.send({
            code: 200,
            data: recommendGoods
        });
    } catch (error) {
        res.send({
            code: 500,
            message: '服务器错误',
            error: error.message
        });
    }
});

module.exports = router;
